package com.attilax.json;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentMap;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.attilax.io.filex;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

public class JsonViewBigFile {
	
	
	private static  Map mapfinal=Maps.newConcurrentMap();

	private static final String spsce = "--";

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String s = "d:\\user sync data.json";
		String str = filex.read(s);
		Object json = JSON.parse(str);
		System.out.println("root:"+json.getClass());
		ConcurrentMap<Object, Object> newConcurrentMap = Maps.newConcurrentMap();
		if (json.getClass() == JSONObject.class) {
			Map m = (Map) json;
	
			printObject(  (Map) json,spsce, newConcurrentMap);
		
		
		}
		System.out.println(AtiJson.toJson( newConcurrentMap));

	}

	private static void printObject(Map m, String space, ConcurrentMap<Object, Object> curMap) {
		Set<String> keys=m.keySet();
		for (String k : keys) {
			Object obj= m.get(k);
			Class<? extends Object> val_class1 =obj.getClass();
			curMap.put(k, val_class1);
			System.out.println( space+"  "+k+":"+val_class1);
		
			if(val_class1==JSONObject.class)
			{
				Map obj2 = (Map) obj;
				ConcurrentMap<Object, Object> newConcurrentMap = Maps.newConcurrentMap();
				curMap.put(k, newConcurrentMap);
				printObject( obj2,space+space,newConcurrentMap );
			}
			else if(val_class1==JSONArray.class)
			{
			   List li=Lists.newArrayList();
				curMap.put(k, li);
				printArr( (List) obj,space+space,li );
			}else
			{
				curMap.put(k, val_class1.getName());
			}
		}
		
	}

	private static void printArr(List arr_para, String space, List li) {
	
		
		List arr=Lists.newArrayList();
		if(arr_para.size()>10)
			arr=arr_para.subList(0, 2);
		for (Object obj : arr) {
			Class<? extends Object> class1 =obj.getClass();
		

			if(class1==JSONObject.class)
			{
				ConcurrentMap<Object, Object> newConcurrentMap = Maps.newConcurrentMap();
			  li.add(newConcurrentMap);
				printObject( (Map) obj,space+spsce,newConcurrentMap );
			}
			
			if(class1==JSONArray.class)
			{
				  List li_new=Lists.newArrayList();
				printArr( (List) obj,space+spsce ,li_new);
			}
		 
		}
		
	}

}
